package com.neu.cloudFactory.service;

import com.neu.cloudFactory.DAO.UserDAO;
import com.neu.cloudFactory.PO.User;
import com.neu.cloudFactory.VO.UserVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserService {
    final UserDAO userDAO;
    public UserService(UserDAO userDAO){
        this.userDAO = userDAO;
    }
    public boolean isExist(String account) {
       return null != userDAO.findByAccount(account);
    }
    public boolean checkPassword(String account, String password){
        return userDAO.findByAccountAndPassword(account, password)!=null;
    }
    public User findUserToLogIn(String account, String password){
        return userDAO.findByAccountAndPassword(account,password);
    }
    public void saveUser(User user){//包括更改和新增
        userDAO.save(user);
    }
    public List<UserVO> findAllUsersVO(){
        System.out.println(userDAO.findAll().toString());
        return userDAO.findAll().stream().map(UserVO::new).collect(Collectors.toList());
    }

    public void deleteUser(User user){
        userDAO.delete(user);
    }

    public User findByAccount(String account){
        return userDAO.findByAccount(account);
    }


}
